package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.vo.SpuInfoSaveVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: SPUController
 * Package: com.atguigu.gmall.product.controller
 * Description:
 *
 * @Author ltystart
 * @Create 2025/8/21 17:34
 * @Version 1.0
 */
@Api(tags = "SPU管理")
@RestController
@RequestMapping("/admin/product")
public class SpuController {
    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    /**
     * 分页获取SPU列表
     *
     * @param category3Id
     * @param pn
     * @param ps
     * @return
     */
    @ApiOperation("分页获取SPU列表")
    @GetMapping("/{pn}/{ps}")
    public Result getSpuPage(@RequestParam("category3Id") Long category3Id,
                             @PathVariable("pn") Long pn,
                             @PathVariable("ps") Long ps) {
        Page<SpuInfo> result = spuInfoService.getSpuInfoPage(category3Id, pn, ps);
        return Result.ok(result);
    }

    /**
     * 保存SPU信息
     *
     * @param spuInfoSaveVo
     * @return
     */
    @ApiOperation("保存SPU信息")
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfoSaveVo spuInfoSaveVo) {
        spuInfoService.saveSpuInfo(spuInfoSaveVo);
        return Result.ok();
    }

    /**
     * 查询SPU的图片列表
     *
     * @param spuId
     * @return
     */
    @ApiOperation(("查询SPU的图片列表"))
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable("spuId") Long spuId) {
        LambdaQueryWrapper<SpuImage> wrapper = new LambdaQueryWrapper<SpuImage>().eq(SpuImage::getSpuId, spuId);
        List<SpuImage> list = spuImageService.list(wrapper);
        return Result.ok(list);
    }

    /**
     * 获取指定SPU定义的所有销售属性名和值
     *
     * @param spuId
     * @return
     */
    @ApiOperation("获取指定SPU定义的所有销售属性名和值")
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrs);
    }
}
